import styles from './index.module.scss';
import { NavBar, TextArea, Toast } from 'antd-mobile';
import { useState } from 'react';
import { useAppDispatch, useAppSelector } from '@/store';
import { getReplyByIdAction, sendCommentAction, sendToComnentAction } from '@/store/action/comment';
import { getArticleDetailAction } from '@/store/action/article';

type Props = {
  // 评论的作者的名字
  // name?: string;
  onBack?: () => void;
};
export default function CommentInput({ onBack }: Props) {
  const dispatch = useAppDispatch();

  const [msg, setMsg] = useState('');

  const { detail } = useAppSelector((state) => state.article);
  const { originComment } = useAppSelector((state) => state.comment);
  const name = originComment.aut_name;

  const handleSend = async () => {
    if (name) {
      await dispatch(
        sendToComnentAction({ content: msg, target: originComment.com_id, art_id: detail.art_id })
      );
      dispatch(getReplyByIdAction(originComment.com_id));
    } else {
      await dispatch(sendCommentAction({ content: msg, target: detail.art_id }));
      dispatch(getArticleDetailAction(detail.art_id));
    }

    onBack && onBack();
    Toast.show({ content: '评论成功' });
  };
  return (
    <div className={styles.root}>
      <NavBar
        onBack={onBack}
        right={
          <span className="publish" onClick={handleSend}>
            发表
          </span>
        }
      >
        {name ? '回复评论' : '评论文章'}
      </NavBar>
      <div className="input-area">
        {/* 回复别人的评论时显示：@某某 */}
        {name && <div className="at">@{name}:</div>}

        {/* 评论内容输入框 */}
        <TextArea value={msg} onChange={setMsg} placeholder="说点什么~" rows={10} />
      </div>
    </div>
  );
}
